package com.template.spring.beerportal.domain;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Version;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@Table(name = "bar")
public class Bar {
	
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    //@Column(name = "id")
	  @Column(name = "BAR_ID", unique = true, nullable = false)
    private Long id;

	@Version
    @Column(name = "version")
    private Integer version;
	
	@OneToOne(fetch = FetchType.LAZY, mappedBy = "bar", cascade = CascadeType.ALL)
	BarDetails barDetails;

   // @ManyToMany(cascade = CascadeType.ALL)
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "bar")
    private Set<Customer> customers = new HashSet<Customer>();
    
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(
  		  name = "BAR_BEER", 
  		  joinColumns = { @JoinColumn(name = "BAR_ID", nullable = false, updatable = false) }, 
  		  inverseJoinColumns = { @JoinColumn(name = "BEER_ID", nullable = false, updatable = false) 
    })
    Set<Beer> beers;
    
    @Column(name = "BAR_NAME", unique = true, nullable = false, length = 128)
    private String barName;    
    
	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	public Long getId() {
        return this.id;
    }

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getVersion() {
		return this.version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public BarDetails getBarDetails() {
		return barDetails;
	}

	public void setBarDetails(BarDetails barDetails) {
		this.barDetails = barDetails;
	}

	public Set<Customer> getCustomers() {
		return this.customers;
	}

	public void setCustomers(Set<Customer> customers) {
		this.customers = customers;
	}

	public Set<Beer> getBeers() {
		return beers;
	}

	public void setBeers(Set<Beer> beers) {
		this.beers = beers;
	}

	public String getBarName() {
		return barName;
	}

	public void setBarName(String barName) {
		this.barName = barName;
	}

	@PersistenceContext
	transient EntityManager entityManager;
	
	public EntityManager getEntityManager() {
        return this.entityManager;
    }

	public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

	public static final EntityManager entityManager() {
        EntityManager em = new Bar().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countBars() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Bar o", Long.class).getSingleResult();
    }

	public static List<Bar> findAllBars() {
        return entityManager().createQuery("SELECT o FROM Bar o", Bar.class).getResultList();
    }

	public static Bar findBar(Long id) {
        if (id == null) return null;
        return entityManager().find(Bar.class, id);
    }

	public static List<Bar> findBarEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Bar o", Bar.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Bar attached = Bar.findBar(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Bar merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Bar merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }


}
